#include <Game.hh>

Game::Game(sf::RenderWindow &win) : GameScreen(win), _Window (win)
{
}

Game::~Game(void)
{
}

void    Game::Initialize(void)
{
    this->_IsRunning = true;
    this->_TargetElapsedTime = 0.16f;
    this->_GameTime = new GameTime();
    this->_FireTime = 0.10f;
    this->_Content = new RessourceManager();
    this->_Timer = 0.0f;

    this->_Player = new Player(this->_Window);
    this->_Background = new Background(this->_Window);
}

void    Game::LoadContent(void)
{
    this->_Content->AddImage("player.png");
    this->_Content->AddImage("background.png");

    const sf::Image &_image = this->_Content->GetImage("player.png");
    sf::Sprite  *_sprite = new sf::Sprite();
    _sprite->SetImage(_image);
    _sprite->SetSubRect(sf::Rect<int>(0, 0, _image.GetWidth() / 2, _image.GetHeight() / 2));
    this->_Player->Initialize(*_sprite, *new sf::Vector2f(10, 10));


    const sf::Image &_image2 = this->_Content->GetImage("background.png");
    sf::Sprite  *_sprite2 = new sf::Sprite();
    _sprite2->SetImage(_image2);
    this->_Background->Initialize(*_sprite2, *new sf::Vector2f(-0.5, 0));
}

int    Game::Run(void)
{
    this->Initialize();
    this->LoadContent();
    while(this->IsRunning()){
        this->_GameTime->UpdateElapsedTime();
        this->_GameTime->UpdateTotalGameTime();
        this->Update(this->_GameTime->ElapsedGameTime());
        this->Draw(this->_GameTime->ElapsedGameTime());
    }
    this->UnLoadContent();
    this->Exit();
    return EXIT;
}

void    Game::Update(Time ElapsedTime)
{
    /** Gestion des events **/
    sf::Event e;
    while (this->_Window.GetEvent(e)){
        if ((e.Type == sf::Event::Closed))
            this->_IsRunning = false;
    }
    _Timer += ElapsedTime;
    if(this->_Window.GetInput().IsKeyDown(sf::Key::Left))
		this->_Player->MoveLeft();
	if(this->_Window.GetInput().IsKeyDown(sf::Key::Right))
		this->_Player->MoveRight();
	if(this->_Window.GetInput().IsKeyDown(sf::Key::Up))
		this->_Player->MoveUp();
	if(this->_Window.GetInput().IsKeyDown(sf::Key::Down))
        this->_Player->MoveDown();
    if(this->_Window.GetInput().IsKeyDown(sf::Key::Escape))
        this->_IsRunning = false;
	if(this->_Window.GetInput().IsKeyDown(sf::Key::Space))
        if (_Timer >= 0.16f){
            this->_Timer = 0.0f;
            this->AddBullet();
        }

    /** Mise a jour position joueur **/
    /*if (this->_GameState == GAME)
    {*/
        this->_Player->Update(ElapsedTime);
        this->UpdateBullets(ElapsedTime);
    /*}*/
    this->_Background->Update(ElapsedTime);

}

void    Game::Draw(Time ElapsedTime)
{
    this->_Window.Clear();
    /** Affichage joueur **/
    this->_Background->Draw(ElapsedTime);
    //if (this->_GameState == GAME)
    //{
        this->DrawBullets(ElapsedTime);
        this->_Player->Draw(ElapsedTime);
    //}
    this->_Window.Display();
}

void    Game::UnLoadContent(void)
{
}

bool    Game::IsRunning()
{
    return this->_IsRunning;
}

void    Game::Exit(void)
{
}

void    Game::AddBullet()
{
    sf::Image   *_image = new sf::Image();
    sf::Sprite  *_sprite = new sf::Sprite();

    if (!_image->LoadFromFile("missile.png"))
    {
        std::cout << "Fichier erreur load" << std::endl;
        exit(1);
    }
    _sprite->SetImage(*_image);

    Bullet  *newBullet = new Bullet(this->_Window);
    sf::Vector2f *_pos = new sf::Vector2f(this->_Player->GetPosition().x + this->_Player->GetSize().x,
                                          this->_Player->GetPosition().y + (this->_Player->GetSize().y / 2));
    newBullet->Initialize(*_sprite, *_pos, sf::Vector2f(2, 0));

    this->_Bullets.push_back(newBullet);
}

void    Game::UpdateBullets(Time ElapsedTime)
{
    std::list<Bullet*>::iterator    it;
    for (it = this->_Bullets.begin(); it != this->_Bullets.end(); ++it)
        (*it)->Update(ElapsedTime);
}

void    Game::DrawBullets(Time ElapsedTime)
{
    std::list<Bullet*>::iterator    it;
    for (it = this->_Bullets.begin(); it != this->_Bullets.end(); ++it)
        (*it)->Draw(ElapsedTime);
}
